/**
 * 
 */
package com.java.test.activity.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.java.test.activity.dto.ActivityManageInfo;
import com.java.test.activity.mapper.ActivityManageMapper;
import com.java.test.activity.service.ActivityManageService;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Service
public class ActivityManageServiceImpl implements ActivityManageService {

	@Autowired
	ActivityManageMapper activityManageMapper;

	@Value("${file.img.down.url}")
	private String fielUrl;

	@Override
	public int insertActivityManageInfo(ActivityManageInfo info) {
		return activityManageMapper.insertActivityManageInfo(info);
	}

	@Override
	public int updateActivityManageInfo(ActivityManageInfo info) {
		return activityManageMapper.updateActivityManageInfo(info);
	}

	@Override
	public int deleteActivityManageInfo(int[] ids) {
		return activityManageMapper.deleteActivityManageInfo(ids);
	}

	@Override
	public PageInfo<ActivityManageInfo> selectactivityList(PageInfo<ActivityManageInfo> page) {
		ActivityManageInfo info = page.getInfo();

		int total = activityManageMapper.selectactivityCount(info);

		List<ActivityManageInfo> list = activityManageMapper.selectactivityList(info, page);

		if (list != null && list.size() > 0) {
			list.forEach(b -> {
				if (b.getImgUrl() != null && !"".equals(b.getImgUrl())) {
					b.setImgUrl(fielUrl + b.getImgUrl());
				}
				LocalDateTime startTime = b.getStartTime();
				LocalDateTime endTime = b.getEndTime();
				LocalDateTime nowTime = LocalDateTime.now();

				int cura = nowTime.compareTo(startTime);
				int curb = nowTime.compareTo(endTime);

				if (cura < 0) {
					b.setStatus(1);
				} else {
					if (curb < 0) {
						b.setStatus(2);
					} else {
						b.setStatus(3);
					}
				}

			});
			if (info.getStatus() != 0) {
				list = list.stream().filter(b -> b.getStatus() == info.getStatus()).collect(Collectors.toList());
			}

		}

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public ActivityManageInfo selectactivityInfo(ActivityManageInfo info) {

		ActivityManageInfo infos = activityManageMapper.selectactivityInfo(info);

		if (infos != null) {
			infos.setImgUrl(fielUrl + infos.getImgUrl());
		}

		return infos;
	}

}
